package mapgen.func {
import mapgen.vo.BarVO;
import mapgen.vo.MapVO;
import mapgen.vo.PosiVO;

import flash.display.MovieClip;

/**
 * @author kangkai
 */
public class MapDataWriterX {
	private var mc_map:MovieClip;
	private var vo:MapVO;
	
	public function MapDataWriterX() {
		
	}
	
	public function out(mc:MovieClip):XML{
		
		vo=new MapVO();
		
		mc_map=mc;
		
		writeData();
		
		return genXML();
	}
	private function genXML():XML{
		var myXML:XML=<map id={vo.id} name={vo.name}></map>;
		myXML.appendChild(<detail>{vo.detail}</detail>);
		myXML.appendChild(<dur_sec>{vo.durSec}</dur_sec>);
		myXML.appendChild(<modes>{vo.modes}</modes>);
		myXML.appendChild(<max_player_count>{vo.maxPlayerCount}</max_player_count>);
		
		myXML.appendChild(<width>{vo.width}</width>);
		myXML.appendChild(<height>{vo.height}</height>);
		myXML.appendChild(<cc>{vo.CC}</cc>);
		myXML.appendChild(<rc>{vo.RC}</rc>);
		
		var barListXML:XML=<bar_list></bar_list>;
		myXML.appendChild(barListXML);
		var ii:int=0;
		for each(var barVO:BarVO in vo.barList){
			var barXML:XML=<bar></bar>;
			barXML.@id=ii++;
			barXML.@type=barVO.type;
			barXML.@x=barVO.x;
			barXML.@y=barVO.y;
			barXML.@z=barVO.z;
			barXML.@rot_y=barVO.rotY;
			barXML.@A=barVO.A;
			barXML.@B=barVO.B;
			barXML.@H=barVO.H;
			barXML.@HP = barVO.HP;
			
			barListXML.appendChild(barXML);
		}
		
		var flowerListXML:XML=<flower_list></flower_list>;
		myXML.appendChild(flowerListXML);
		for each(var posiVO:PosiVO in vo.flowerList){
			var flowerXML:XML=<posi></posi>;
			
			flowerXML.@x=posiVO.x;
			//flowerXML.@y=posiVO.y;
			flowerXML.@z=posiVO.z;
			//flowerXML.@rotY=posiVO.rotY;
		
			
			flowerListXML.appendChild(flowerXML);
		}
		
		var teamAListXML:XML=<team_red_list></team_red_list>;
		myXML.appendChild(teamAListXML);
		for each(var posiVO:PosiVO in vo.teamAList){
			var teamAXML:XML=<posi></posi>;
			
			teamAXML.@x=posiVO.x;
			//teamAXML.@y=posiVO.y;
			teamAXML.@z=posiVO.z;
			teamAXML.@rot_y=posiVO.rotY;
		
			
			teamAListXML.appendChild(teamAXML);
		}
		
		var teamBListXML:XML=<team_blue_list></team_blue_list>;
		myXML.appendChild(teamBListXML);
		for each(var posiVO:PosiVO in vo.teamBList){
			var teamBXML:XML=<posi></posi>;
			
			teamBXML.@x=posiVO.x;
			//teamBXML.@y=posiVO.y;
			teamBXML.@z=posiVO.z;
			teamBXML.@rot_y=posiVO.rotY;
		
			
			teamBListXML.appendChild(teamBXML);
		}
		
		var teamFreeListXML:XML=<team_free_list></team_free_list>;
		myXML.appendChild(teamFreeListXML);
		for each(var posiVO:PosiVO in vo.teamFreeList){
			var teamFreeXML:XML=<posi></posi>;
			
			teamFreeXML.@x=posiVO.x;
			//teamFreeXML.@y=posiVO.y;
			teamFreeXML.@z=posiVO.z;
			teamFreeXML.@rot_y=posiVO.rotY;
		
			
			teamFreeListXML.appendChild(teamFreeXML);
		}
		
		return myXML;
	}
	
	public function writeData() : String {
		
		sOut="";
		//地图信息
		vo.id=int(mc_map.tf_id.text);
		vo.name=mc_map.tf_name.text;
		vo.detail=mc_map.tf_detail.text;
		vo.maxPlayerCount=int(mc_map.tf_max.text);
		
		vo.modes=mc_map.tf_mode.text;
		vo.durSec=int(mc_map.tf_sec.text);
		
		vo.width=int(mc_map.mc_bg.width);//大小
		vo.height=int(mc_map.mc_bg.height);
		vo.CC = vo.width/64;
		vo.RC = vo.height/64;
		//outputDD("#");

		//障碍物信息
		
		findBars();
		//outputDD("#");
		//个玩家初始位置
		
		findProp();
		
		findTeamA();
		findTeamB();
		findTeamFree();
		//outputDD("#");
		
		
		return sOut;
	}
	
	private var sOut:String="";
	private function outputDD(s : String) : void {
		sOut+=s+"\n";
	}

	//=========================函数===============
	//找出障碍物
	private function findBars():void {
		
		for(var i : int = 0;i < mc_map.numChildren;i++) {
			var mcc : MovieClip = mc_map.getChildAt(i) as MovieClip;
			
			if(mcc == null) {
				continue;
			}
			if(mcc.mc_wh == null) {
				continue;
			}
			
			var outX : int = mcc.x;
			var outZ : int = -mcc.y;

			var CellA : int = mcc.mc_wh.width;
			var CellB : int = mcc.mc_wh.height;


			var rot : int = mcc.rotation;
			if(rot < 0) {
				rot += 360;
			}
			
			//注意这一步是因为我碰撞检测的时候不会考虑旋转，所以ABH要做好
			var outA : int = CellA;
			var outB : int = CellB;
			if(rot == 90 || rot == 270) {
				outA = CellB;
				outB = CellA;
			}
			
			//注意高度以后是要x2的，因为是以底面中心做碰撞的
			var type : int = parseInt(mcc.tf_type.text);
			var outH : int = parseInt(mcc.tf_height.text) ;
			var hp : int = parseInt(mcc.tf_hp.text);
	
			//根据类型，排放位置
			var outY : int = 0;//底面中心的y
			//如果是坑，则放低,就是把东西放到地平线以下，
			//这样，坑就只被坦克碰撞而不被炮弹碰撞了
			if(type == 0) {
				outY = -outH / 2;
			}
				
			//输出
			var barVO:BarVO=new BarVO();
			barVO.type=type;
			
			barVO.x=outX;
			barVO.y=outY;
			barVO.z=outZ;
			barVO.A=outA;
			barVO.B=outB;
			barVO.H=outH;
			
			barVO.HP=hp;
				
			vo.barList.push(barVO);//-
			
			outputDD(type + "," + hp + "," + outX + "," + outY + "," + outZ + "," + outA + "," + outB + "," + outH + "," + rot);
		}
	}

	//找出A队队员
	private function findTeamA():void {

		for(var i : int = 0;i < mc_map.numChildren;i++) {
			var mcc : MovieClip = mc_map.getChildAt(i) as MovieClip;
			if(mcc == null) {
				continue;
			}
			if(mcc.tf_teamA == null) {
				continue;
			}

			var outX : int = mcc.x;
			var outZ : int = -mcc.y;


			var rot : int = mcc.rotation;
			if(rot < 0) {
				rot += 360;
			}
			
			var posiVO:PosiVO=new PosiVO();
			posiVO.x=outX;
			posiVO.z=outZ;
			posiVO.rotY=rot;
			vo.teamAList.push(posiVO);


			outputDD(outX + "," + outZ + "," + rot);
		}
	}

	//找出B队队员
	private function findTeamB():void {
		for(var i : int = 0;i < mc_map.numChildren;i++) {
			var mcc : MovieClip = mc_map.getChildAt(i) as MovieClip;
			if(mcc == null) {
				continue;
			}
			if(mcc.tf_teamB == null) {
				continue;
			}

			var outX : int = mcc.x;
			var outZ : int = -mcc.y;


			var rot : int = mcc.rotation;
			if(rot < 0) {
				rot += 360;
			}

			var posiVO:PosiVO=new PosiVO();
			posiVO.x=outX;
			posiVO.z=outZ;
			posiVO.rotY=rot;
			vo.teamBList.push(posiVO);

			outputDD(outX + "," + outZ + "," + rot);
		}
	}

	//找出自由人位置
	private function findTeamFree():void {
		for(var i : int = 0;i < mc_map.numChildren;i++) {
			var mcc : MovieClip = mc_map.getChildAt(i) as MovieClip;
			if(mcc == null) {
				continue;
			}
			if(mcc.tf_free == null) {
				continue;
			}

			var outX : int = mcc.x;
			var outZ : int = -mcc.y;


			var rot : int = mcc.rotation;
			if(rot < 0) {
				rot += 360;
			}
			
			var posiVO:PosiVO=new PosiVO();
			posiVO.x=outX;
			posiVO.z=outZ;
			posiVO.rotY=rot;
			vo.teamFreeList.push(posiVO);
			
			outputDD(outX + "," + outZ + "," + rot);
		}
	}

	//找出生道具的位置
	private function findProp():void{
		for(var i : int = 0;i < mc_map.numChildren;i++) {
			var mcc : MovieClip = mc_map.getChildAt(i) as MovieClip;
			if(mcc == null) {
				continue;
			}
			if(mcc.tf_prop == null) {
				continue;
			}

			var outX : int = mcc.x;
			var outZ : int = -mcc.y;
			
			var posiVO:PosiVO=new PosiVO();
			posiVO.x=outX;
			posiVO.z=outZ;
			//posiVO.rotY=rot;
			vo.flowerList.push(posiVO);
			
			outputDD(outX + "," + outZ);
		}
	}
}
}
